#!/usr/bin/python
# -*- coding: UTF-8 -*-

import xml.sax

from core.entity import ConfigData as cdb

# 读取config.xml，获取数据源信息
class CountryHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.CurrentData = ''
        self.CurrentAttributes = ''

    def startElement(self, tag, attributes):
        self.CurrentData = tag
        self.CurrentAttributes = attributes
        if tag == "datasource":
            dbtype = attributes["type"]
            cdb.dbtype = dbtype
        elif tag == "excel":
            path = attributes["path"]
            cdb.excelpath = path

    def endElement(self, tag):
        self.CurrentData = ""

    def characters(self, content):
        if self.CurrentData == "url":
            cdb.url = content
        elif self.CurrentData == "username":
            cdb.username = content
        elif self.CurrentData == "password":
            cdb.password = content
        elif self.CurrentData == "db":
            cdb.db = content
        elif self.CurrentData == "port":
            cdb.port = content

    def getConfig(self):
        parser = xml.sax.make_parser()
        parser.setFeature(xml.sax.handler.feature_namespaces, 0)
        Handler = CountryHandler()
        parser.setContentHandler(Handler)
        parser.parse("config.xml")
        return cdb